 #ifndef DATABASE_H
#define DATABASE_H

#include "stdlib.h"
#include "stdio.h"
/*
 *	This file contains the utilities in the GPUQP project
 *  http://ihome.ust.hk/~saven/gpuqp.html
 */

/*
 *	Hardware parameters
 */
//CPU
#define CPU_CACHE_SIZE (256*1024)//1024*1024 bytes
#define CPU_BLOCK_SIZE 128//bytes

//GPU
#define GPU_CACHE_SIZE 8096
#define GPU_BLOCK_SIZE 64 //8*8

#include <cutil.h>
/************************************************************************/
/*  for testing
/************************************************************************/
#define TEST_MAX ((1<<30)-1)
typedef enum{
	BLK_NLJ,
		INDEX_NLJ,
		SM_JOIN,
		HASH_JOIN
} JOIN_MODE;

#define MIN(x,y) ((x)>(y)?(y):(x))
#define TEST_MAX ((1<<30)-1)

#define NLJ_BLOCK_SIZE (CPU_CACHE_SIZE/4/sizeof(int2))

#define NUM_THREAD_PER_BLOCK 256

//#define DEBUG_SAVEN 1

void __DEBUG__(int *R,int rLen)
{
#ifdef DEBUG_SAVEN
	for(int i=0;i<rLen;i++)
	{
		printf("%d, ", R[i]);
		if(i%10==9) printf("\n");
	}
	printf("\n");
#endif
}

void gpuPrint(int *d_output, int numResults, char *notes)
{
#ifdef DEBUG_SAVEN
	printf("----------%s------------\n", notes);
	int result=0;
	int *h_output=(int *)malloc(sizeof(int)*numResults);
	CUDA_SAFE_CALL( cudaMemcpy( h_output, d_output, numResults*sizeof(int) , cudaMemcpyDeviceToHost) );
	for(int i=0;i<numResults;i++)
	{
		printf("%d, ", h_output[i]);
		result+=h_output[i];
		if(i%10==9) printf("\n");
	}
	printf("#results in GPU: %f K, length, %d\n",(double)result, numResults);
	free(h_output);
#endif

}

void gpuPrintInt2(int2 *d_output, int numResults, char *notes)
{
#ifdef DEBUG_SAVEN
	printf("----------%s------------\n", notes);
	int result=0;
	int2 *h_output=(int2 *)malloc(sizeof(int2)*numResults);
	CUDA_SAFE_CALL( cudaMemcpy( h_output, d_output, numResults*sizeof(int2) , cudaMemcpyDeviceToHost) );
	for(int i=0;i<numResults;i++)
	{
		//printf("[%d,%d], ", h_output[i].x, h_output[i].y);
		printf("%d, ", h_output[i].y);
		result+=h_output[i].y;
		if(i%10==9) printf("\n");
	}
	printf("#results in GPU: %f K, length, %d\n",(double)result, numResults);
	free(h_output);
#endif
}

#endif

